package org.example.springbootdemo;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/1/22 11:19
 * @description:
 */
@RequestMapping("/message")
@RestController
public class MessageController {
    List<MessageInfo> list = new ArrayList<>();
//    @RequestMapping(value = "/publish", produces = "application/json")
    @RequestMapping("/publish")
    // * @RequestBody 从请求报文的body中获取数据
    public String publish (@RequestBody MessageInfo messageInfo) {
        if (!StringUtils.hasLength(messageInfo.getFrom()) ||
            !StringUtils.hasLength(messageInfo.getTo()) ||
            !StringUtils.hasLength((messageInfo.getMessage()))) {
            return "{\"ok\":0}";
        }
        list.add(messageInfo);
        return "{\"ok\":1}";
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return list;
    }
}
